package com.special.threademo.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liuYC
 * @ClassName ReentrantLockDemo
 * @Description TODO
 * @date 2021/4/26 17:08
 * todo   https://www.cnblogs.com/qlqwjy/p/10130454.html
 * <p>
 * 用法：
 * 1 lock和unlock的使用，地点基本的
 * 2 使用Condition实现等待/通知
 * 3 使用多个Condition实现等待/通知部分线程
 * 4 公平锁与非公平锁
 * 5  使用condition实现线程按顺序执行
 * 6 其他方法研究
 */
public class ReentrantLockDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReentrantLockDemo.class);
    private Lock lock = new ReentrantLock();
    private Condition condition1 = lock.newCondition();

    public void testMethod() {
        try {
            LOGGER.info("threadName -> {} enter testMethod", Thread.currentThread().getName());
            lock.lock();
//            int i = 1 / 10;
            LOGGER.info("threadName -> {} lock", Thread.currentThread().getName());
            Thread.sleep(2 * 1000);

        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            LOGGER.info("threadName -> {} unlock", Thread.currentThread().getName());
            lock.unlock();
        }
        LOGGER.info("threadName -> {} exit testMethod", Thread.currentThread().getName());
    }

    public static void main(String[] args) {
        new ReentrantLockDemo().testMethod();
    }


}
